#!/usr/bin/php
<?php

mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');

define('CWD', dirname(__FILE__));

require_once CWD.'/../../dominator/classhelper/MockTestCase.class.php';
require_once CWD.'/../../dominator/driver/SlaveDriver.class.php';

function text($xpath) {
	global $d;
	if ($d->isElementPresent($xpath))
		return $d->getText($xpath);
	return '!404!';
}

$words = file(CWD.'/words.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$words = array_unique($words);
$textPattern = '/\pL{2,}/u';
$result = fopen(CWD.'/output.csv', 'w');

$d = new SlaveDriver();
$d->setTimeout(30);
$d->setBrowserUrl('http://lingvo.yandex.ru/');
$d->start();
foreach ($words as $word) {
	if ($word[0] == '#')
		continue;
	
	echo "\nProcessing $word";
	$unit = array('word' => $word);
	$d->open('/en?text='.$word);
//	if (!$d->isElementPresent('//span[@class="uni"]')) {
//		$fluctuated = true;
//		$d->clickAndWait('//div[contains(@class, "lingvo-article")]/div[not(@class)]/div/a');
//	}
//	$unit['transcription'] = '['.$d->getText('//span[@class="uni"]').']';
	$unit['transcription'] = '['.text('//span[@class="uni"]').']';
	if (isset($fluctuated)) $d->open('/en?text='.$word);
	$link = 'link=Компьютеры';
	if ($d->isElementPresent($link)) {
		$d->clickAndWait($link);
	} else {
		echo ' — not computing';
	}
	for ($i = 1; $i<3; $i++) {
		$p = '//p[@class="L'.$i.'"]';
		if (!$d->isElementPresent($p)) {
			throw new Exception($p.' not present for "'.$word.'"');
		}
		$text = $d->getText($p);
		if (preg_match_all($textPattern, $text, $matches)) {
			$unit['translation'] = implode(', ', $matches[0]);
			break;
		}
	}
	$d->open('http://www.ldoceonline.com/dictionary/'.$word);
	$unit['explanation'] = text('//span[@class="DEF"]');
	do {
		$d->open('http://www.google.com/search?q='.$word.'+computing&hl=en');
	} while($d->isTextPresent('but your computer or network may be sending automated queries') && !sleep(5));
	$links = array();
	while (empty($unit['sentence'])) {
		for ($i = 1; $i < $d->getXpathCount('//li'); $i++) {
			$div = '//li['.$i.']/div[@class="s"]';
			if ($d->isElementPresent($div)) {
				$text = $d->getText($div);
				preg_match('/[A-Z].*?[.!?](?=\s[A-Z])/', $text, $matches);
				foreach ($matches as $match) {
					if (substr($match, -3) != '...') {
						$unit['sentence'] = $match;
						break 2;
					}
				}
			}
		}
		$d->clickAndWait('link=Next');
	}
	fputcsv($result, $unit);
}
$d->stop();
fclose($result);
